﻿@using TD.Common.Kendo.Mvc5.Scheduler

@(Html.Kendo().Scheduler<TD.CTS.WebUI.Models.TaskSchedulerEvent>()
    .Month("TasksScheduler")
    .EventTemplate(@"<div><pre>#= title #</pre></div>")
    .DataSource(source => source
        .Read(read => read.Action("GetTasks", "Monitoring").Data("GetDates"))
        .ServerOperation(true)
        .Events(events => events.Error("kendo_error_handler"))
    )
    .Events(events => events.DataBound("DataBound"))
)

@*(Html.Kendo().ContextMenu()
    .Name("proceduresMenu")
    .Target("#TasksScheduler")
    .Filter(".td-task")
    .ShowOn("click")
    //.AlignToAnchor(true)
    .Items(items => { items.Add(); })
    .Events(e => e.Open("ProceduresMenuOpen"))
)*@

<script>
    var viewMode = "visit";
    $(document).ready(function () {
        $(".k-scheduler-views").html('<li class="k-state-default td-view" view-mode="visit">' +
            '<a role="button" href="#" class="k-link">По визитам</a></li>' +
            '<li class="k-state-default td-view" view-mode="procedure"><a role="button" href="#" class="k-link">По процедурам</a></li>');

        $(".k-scheduler-views .td-view").click(function () {
            viewMode = $(this).attr("view-mode");
            var scheduler = $("#TasksScheduler").data("kendoScheduler");
            scheduler.dataSource.read();
        });

        var scheduler = $("#TasksScheduler");
        scheduler.on("click", ".k-event", function () {
            var uid = $(this).attr("data-uid");
            var events = $("#TasksScheduler").data("kendoScheduler")._data;
            for (var i = 0; i < events.length; i++) {
                if (events[i].uid === uid) {
                    $().redirect("@Url.Action("VisitListByDay", "Monitoring")",
                        { date: kendo.toString(events[i].start, "yyyy-MM-dd") });
                }
            }
        });

        SetContentBlock(scheduler);
    });

    function GetDates() {
        var view = $("#TasksScheduler").data("kendoScheduler").view();

        return {
            visitDateBegin: view.startDate().toISOString(),
            visitDateEnd: view.endDate().toISOString(),
            viewMode: viewMode
        };
    }

    function DataBound(e) {
        $(".k-scheduler-views .td-view").removeClass("k-state-selected");
        $(".k-scheduler-views .td-view[view-mode='" + viewMode + "']").addClass("k-state-selected");
        @*$("#TasksScheduler").find(".td-task").each(function () {
            var id = $(this).attr("href");
            $(this).attr("href", "@Url.Action("Details", "Tasks")" + id);
        });*@
    }

    @*function ProceduresMenuOpen(e) {
        var target = $(e.target);
        var procs = target.attr("procs").split(/, /);
        var ids = target.attr("ids").split(/,/);
        var menu = $("#proceduresMenu").data("kendoContextMenu");
        menu.element.children("li").each(function (i, item) {
            menu.remove(item);
        });
        for (var i = 0; i < procs.length; i++){
            menu.append({ text: procs[i], url: "@Url.Action("Details", "Tasks")"+ids[i] });
        }
    }*@
</script>

<style>
    .k-event {
        height: 60px !important;
        overflow-y: auto;
        overflow-x: hidden;
        background: transparent;
        border-style: none;
        cursor: pointer;
    }

    .td-task-undone {
        color: blue !important;
    }

    .td-task-done {
        color: green !important;
    }

    .td-task-overdue {
        color: red !important;
    }

    pre {
        font-size: 1.2em;
    }
</style>